package com.yrk.controller;

import com.yrk.entity.User;
import com.yrk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userServiceImpl;
    //登录
    @RequestMapping("/login")
    public int login(@RequestBody User user) {
        User user1=userServiceImpl.loginUser(user);
        System.out.println(user1);
        if (user.getPassword().equals(user1.getPassword())) {
            return user1.getRoleid();
        }else{
            return -1;
        }
    }
    //注册
    @RequestMapping("/regist")
    public String regist(@RequestBody User user) {
        int num=userServiceImpl.registerUser(user);
        if (num>0) {
            return "true";
        } else{
            return "false";
        }
    }
    //增加
    @RequestMapping("/addUser")
    public int addUser(@RequestBody User user) {
        return userServiceImpl.addUser(user);
    }
    //删除
    @RequestMapping("/daleteUser")
    public int deleteUser(int user_id) {
        User user = new User();
        user.setUser_id(user_id);
        return userServiceImpl.deleteUser(user);
    }
    //修改
    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody User user) {
        int num=userServiceImpl.updateUser(user);
        if (num>0) {
            return "true";
        }else{
            return "false";
        }
    }
    //查询全部
    @RequestMapping("/selectAll")
    public List<User> selectAll() {
        return userServiceImpl.getUserList();
    }
    //模糊查询
    @RequestMapping("/findByVague")
    public List<User> findUserByVague(String username) {
        return userServiceImpl.findUserByVague(username);
    }
    //批量删除
    @RequestMapping("/deleteInBulk")
    public String deleteInBulk(@RequestBody List<Integer> userList) {
        int num=userServiceImpl.deleteInBulkUser(userList);
        if (num>0) {
            return "true";
        }else{
            return "false";
        }
    }
    //分页查询
    @RequestMapping("/findUserPage")
    public Map<String,Object> findUserPage(@RequestBody Map<String, Integer> map){
        return userServiceImpl.findUserPage(map);
    }
}
